package com.love.travel.service.impl;

import java.util.List;
import com.love.common.core.utils.DateUtils;
import com.love.common.core.utils.snow.SnowflakeIdGenerator;
import com.love.travel.domain.TravelItineraryAttraction;
import com.love.travel.mapper.TravelItineraryAttractionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.love.travel.mapper.TravelProductItineraryMapper;
import com.love.travel.domain.TravelProductItinerary;
import com.love.travel.service.ITravelProductItineraryService;

/**
 * 产品行程Service业务层处理
 * 
 * @author love
 * @date 2025-10-24
 */
@Service
public class TravelProductItineraryServiceImpl implements ITravelProductItineraryService 
{
    @Autowired
    private TravelProductItineraryMapper travelProductItineraryMapper;
    @Autowired
    private TravelItineraryAttractionMapper travelItineraryAttractionMapper;
    private final SnowflakeIdGenerator idGenerator = new SnowflakeIdGenerator(3, 3);

    /**
     * 查询产品行程
     * 
     * @param itineraryId 产品行程主键
     * @return 产品行程
     */
    @Override
    public TravelProductItinerary selectTravelProductItineraryByItineraryId(Long itineraryId)
    {
        return travelProductItineraryMapper.selectTravelProductItineraryByItineraryId(itineraryId);
    }

    /**
     * 查询产品行程列表
     * 
     * @param travelProductItinerary 产品行程
     * @return 产品行程
     */
    @Override
    public List<TravelProductItinerary> selectTravelProductItineraryList(TravelProductItinerary travelProductItinerary)
    {
        return travelProductItineraryMapper.selectTravelProductItineraryList(travelProductItinerary);
    }

    /**
     * 新增产品行程
     * 
     * @param travelProductItinerary 产品行程
     * @return 结果
     */
    @Override
    public int insertTravelProductItinerary(TravelProductItinerary travelProductItinerary)
    {
        travelProductItinerary.setItineraryId(idGenerator.nextId());
        travelProductItinerary.setCreateTime(DateUtils.getNowDate());
        if(travelProductItinerary.getAttractionIds()!=null){
            for(Long attractionId:travelProductItinerary.getAttractionIds()){
                TravelItineraryAttraction travelItineraryAttraction = new TravelItineraryAttraction();
                travelItineraryAttraction.setItineraryId(travelProductItinerary.getItineraryId());
                travelItineraryAttraction.setAttractionId(attractionId);
                this.travelItineraryAttractionMapper.insertTravelItineraryAttraction(travelItineraryAttraction);
            }
        }
        return travelProductItineraryMapper.insertTravelProductItinerary(travelProductItinerary);
    }

    /**
     * 修改产品行程
     * 
     * @param travelProductItinerary 产品行程
     * @return 结果
     */
    @Override
    public int updateTravelProductItinerary(TravelProductItinerary travelProductItinerary)
    {
        travelProductItinerary.setUpdateTime(DateUtils.getNowDate());
        if(travelProductItinerary.getAttractionIds()!=null){
            this.travelItineraryAttractionMapper.deleteTravelItineraryAttractionByItineraryId(travelProductItinerary.getItineraryId());
            for(Long attractionId:travelProductItinerary.getAttractionIds()){
                TravelItineraryAttraction travelItineraryAttraction = new TravelItineraryAttraction();
                travelItineraryAttraction.setItineraryId(travelProductItinerary.getItineraryId());
                travelItineraryAttraction.setAttractionId(attractionId);
                this.travelItineraryAttractionMapper.insertTravelItineraryAttraction(travelItineraryAttraction);
            }
        }
        return travelProductItineraryMapper.updateTravelProductItinerary(travelProductItinerary);
    }

    /**
     * 批量删除产品行程
     * 
     * @param itineraryIds 需要删除的产品行程主键
     * @return 结果
     */
    @Override
    public int deleteTravelProductItineraryByItineraryIds(Long[] itineraryIds)
    {
        return travelProductItineraryMapper.deleteTravelProductItineraryByItineraryIds(itineraryIds);
    }

    /**
     * 删除产品行程信息
     * 
     * @param itineraryId 产品行程主键
     * @return 结果
     */
    @Override
    public int deleteTravelProductItineraryByItineraryId(Long itineraryId)
    {
        return travelProductItineraryMapper.deleteTravelProductItineraryByItineraryId(itineraryId);
    }
}
